//
//  XxwsVC.swift
//  Auscultation
//
//  Created by 来 on 2021/1/7.
//

import UIKit

class XxwsVC: BaseVC {
    
    // 1 完善信息 2 个人编辑
    var type: Int = 1
    
    var scrollView: UIScrollView!
    
    var tx: LGFLoginImageTitleView!
    var tx_i: UIImageView!
    var xm: LGFLoginImageTitleView!
    var xb: LGFLoginImageTitleView!
    var sfz: LGFLoginImageTitleView!
    var sjh: LGFLoginImageTitleView!
    var csrq: LGFLoginImageTitleView!
    var mz: LGFLoginImageTitleView!
    var zy: LGFLoginImageTitleView!
    var js: LGFLoginImageTitleView!
    var ssyy: LGFLoginImageTitleView!
    var zc: LGFLoginImageTitleView!
    var xl: LGFLoginImageTitleView!
    var xw: LGFLoginImageTitleView!
    var zsbh: LGFLoginImageTitleView!
    var dzyx: LGFLoginImageTitleView!
    var dq: LGFLoginImageTitleView!
    var xxdz: LGFLoginImageTitleView!

    var sfzxxm: LGFLoginImageTitleView!
    var sfzxxm_i: UIImageView!
    var sfzghm: LGFLoginImageTitleView!
    var sfzghm_i: UIImageView!
    var zgzs: LGFLoginImageTitleView!
    var zgzs_i: UIImageView!
    
    var bc: UIButton!
    
    var idcardPhoto: String = ""
    var certificate: String = ""
    
    var xlArr: [[String: Any]] = []
    var zyArr: [[String: Any]] = []
    var bznrArr: [[String: Any]] = []
    var ssyyArr: [[String: Any]] = []
    var zcArr: [[String: Any]] = []
    var xwArr: [[String: Any]] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        initUI()
        if type == 2 {
            getData()
        }
        getData2()
    }
    
    @objc func s_bc() {

        if xm.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请填写姓名")
            return
        }
        if sfz.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请填写身份证号码")
            return
        }
        if !sfz.textField.text!.lgf_NSPredicate("(^\\d{15}$)|(^\\d{17}([0-9]|X|x)$)") {
            SVProgressHUD.showError("请填写正确身份证号码")
            return
        }
        if type == 2 {
            if sjh.textField.text!.lgf_IsBlank() {
                SVProgressHUD.showError("请填写手机号码")
                return
            }
            if !sjh.textField.text!.lgf_NSPredicate("^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$") && !sjh.textField.text!.contains("****") {
                SVProgressHUD.showError("请填写正确手机号码")
                return
            }
        }
        if zy.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请选择专业")
            return
        }
        if ssyy.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请选择所属医院")
            return
        }
        if zc.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请选择职称")
            return
        }
        if xl.textField.text!.lgf_IsBlank() {
            SVProgressHUD.showError("请选择学历")
            return
        }
        if type == 1 {
            if zsbh.textField.text!.lgf_IsBlank() {
                SVProgressHUD.showError("请填写证书编号")
                return
            }
            if sfzxxm.lgf_Name == nil {
                SVProgressHUD.showError("请选择身份证信息面图片")
                return
            }
            if sfzghm.lgf_Name == nil {
                SVProgressHUD.showError("请选择身份证国徽面图片")
                return
            }
            if zgzs.lgf_Name == nil {
                SVProgressHUD.showError("请选择资格证书图片")
                return
            }
        }
        if type == 1 {
            certificate = zsbh.textField.text ?? ""
            idcardPhoto = [(sfzxxm.lgf_Name ?? ""), (sfzghm.lgf_Name ?? ""), (zgzs.lgf_Name ?? "")].joined(separator: ",")
        }
        var paramet: [String: Any] = [
            "realName" : xm.textField.text ?? "",
            "gender" : xb.lgf_Name ?? "",
            "idCard" : sfz.textField.text ?? "",
            "birthday" : (csrq.textField.text! + " 00:00:00"),
            "nation": mz.textField.text ?? "",
            "specialty": zy.lgf_Name ?? "",
            "role": ["id": "3124296998191104"],
            "hospital": ["id": ssyy.lgf_Name ?? ""],
            "position": zc.lgf_Name ?? "",
            "education": xl.lgf_Name ?? "",
            "degree": xw.lgf_Name ?? "",
            "email": dzyx.textField.text ?? "",
            "address" : dq.textField.text ?? "",
            "certificate": certificate,
            "detailAddress" : xxdz.textField.text ?? "",
            "idcardPhoto": idcardPhoto,
            "avatar": tx.lgf_Name ?? "",
        ]
        if type == 2 {
            paramet["phone"] = sjh.textField.text ?? ""
        }
        if type == 1 {
            EWERequest.lgf("signup/" + EWEUser.lgf.wsxxuserId, paramet, .put, JSONEncoding.default) { [weak self] (data) in
                SVProgressHUD.showSuccess("保存成功")
                self?.navigationController?.popViewController(animated: true)
            } _: { (error) in
                SVProgressHUD.showError(error)
            }
        } else if type == 2 {
            EWERequest.lgf("user/" + EWEUser.lgf.userId, paramet, .put, JSONEncoding.default) { [weak self] (data) in
                SVProgressHUD.showSuccess("更新成功")
                self?.navigationController?.popViewController(animated: true)
            } _: { (error) in
                SVProgressHUD.showError(error)
            }
        }
    }
    
    func getData() {
        let paramet: [String: Any] = [
            "encryptedData": "L1tqc+noiBVTnGsxwTstlQ=="
        ]
        EWERequest.lgf("user/" + EWEUser.lgf.userId, paramet, .get) { [weak self] (data) in
            self?.tx.lgf_Name = (data["avatar"] as? String) ?? ""
            self?.tx_i.lgf_setImage(self?.tx.lgf_Name ?? "")
            self?.xm.textField.text = (data["realName"] as? String) ?? ""
            let gender = (data["gender"] as? Int) ?? 0
            self?.xb.textField.text = ["男", "女"][gender]
            self?.xb.lgf_Name = gender.description
            self?.sfz.textField.text = (data["idcard"] as? String) ?? ""
            self?.sjh.textField.text = (data["phone"] as? String) ?? ""
            self?.csrq.text = ((data["birthday"] as? Int) ?? 0).description.lgf_TimeStampToDate().lgf_TimeStr("yyyy-MM-dd")
            self?.csrq.lgf_Name = ((data["birthday"] as? Int) ?? 0).description
            self?.mz.textField.text = (data["nation"] as? String) ?? ""
            self?.zy.lgf_Name = (data["specialty"] as? String) ?? ""
            self?.zy.textField.text = (data["specialtyName"] as? String) ?? ""
            let role = (data["role"] as? [String: String]) ?? [:]
            if role.count > 0 {
                self?.js.lgf_Name = role["id"]
                self?.js.textField.text = role["name"]
            }
            let hospital = (data["hospital"] as? [String: String]) ?? [:]
            if hospital.count > 0 {
                self?.ssyy.lgf_Name = hospital["id"]
                self?.ssyy.textField.text = hospital["hospitalName"]
            }
            self?.zc.lgf_Name = (data["position"] as? String) ?? ""
            self?.zc.textField.text = (data["positionName"] as? String) ?? ""
            self?.xl.lgf_Name = (data["education"] as? String) ?? ""
            self?.xl.textField.text = (data["educationName"] as? String) ?? ""
            self?.xw.lgf_Name = (data["degree"] as? String) ?? ""
            self?.xw.textField.text = (data["degreeName"] as? String) ?? ""
            self?.dzyx.textField.text = (data["email"] as? String) ?? ""
            self?.dq.textField.text = (data["address"] as? String) ?? ""
            self?.xxdz.textField.text = (data["detailAddress"] as? String) ?? ""
            self?.idcardPhoto = (data["idcardPhoto"] as? String) ?? ""
            self?.certificate = (data["certificate"] as? String) ?? ""
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
    }
    
    func getData2() {
        // 学历
        let paramet1: [String: Any] = [
            "groupCode": "EMPLOYEE_EDUCATION",
        ]
        EWERequest.lgf("dictionary", paramet1, .get) { [weak self] (data) in
            self?.xlArr = data["result"] as? [[String: Any]] ?? []
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
        
        // 专业
        let paramet2: [String: Any] = [
            "groupCode": "EMPLOYEE_SPECIALTY",
        ]
        EWERequest.lgf("dictionary", paramet2, .get) { [weak self] (data) in
            self?.zyArr = data["result"] as? [[String: Any]] ?? []
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
        
        // 所属医院
        let paramet3: [String: Any] = [
            "limit": 100,
        ]
        EWERequest.lgf("hospitalMessage", paramet3, .get) { [weak self] (data) in
            self?.ssyyArr = data["result"] as? [[String: Any]] ?? []
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
        
        // 职称
        let paramet4: [String: Any] = [
            "groupCode": "EMPLOYEE_POSITION",
        ]
        EWERequest.lgf("dictionary", paramet4, .get) { [weak self] (data) in
            self?.zcArr = data["result"] as? [[String: Any]] ?? []
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
        
        // 学位
        let paramet5: [String: Any] = [
            "groupCode": "EMPLOYEE_DEGREE",
        ]
        EWERequest.lgf("dictionary", paramet5, .get) { [weak self] (data) in
            self?.xwArr = data["result"] as? [[String: Any]] ?? []
        } _: { (error) in
            SVProgressHUD.showError(error)
        }
        
    }
    
}
